#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::shared_ptr;

class Child;

class Parent{
public:
    Parent(){
        cout << "Parent()" << endl;
    }
    ~Parent(){
        cout << "~Parent()" << endl;
    }
    shared_ptr<Child> _spChild;
    //weak_ptr<Child> _spChild;
};
class Child{
public:
    Child(){
        cout << "Child()" << endl;
    }
    ~Child(){
        cout << "~Child()" << endl;
    }
    shared_ptr<Parent> _spParent;
    //weak_ptr<Parent> _spParent;

};

void test(){
    shared_ptr<Parent> spParent(new Parent());
    shared_ptr<Child> spChild(new Child());
    //循环指向
    spParent->_spChild = spChild;
    spChild->_spParent = spParent;
    //函数结束后，两个指针的引用计数均为1，因此不会被释放
    //解决办法：将类中的shared_ptr改为weak_ptr;
}

int main()
{
    

    test ();
    return 0;
}

